//
//  UIWindow+Extension.m
//  JiLaiBao
//
//  Created by youmy on 15/9/3.
//  Copyright (c) 2015年 youmy. All rights reserved.
//

#import "UIWindow+Extension.h"
#import "PinGJByOwnerSide-Swift.h"


@implementation UIWindow (Extension)

- (void)switchRootViewController{
    // 切换窗口的根控制器
    NSString * key = @"CFBundleVersion";
    // 当前软件的版本号（从inf.pist中获得）
    NSString * currentVersion = [NSBundle mainBundle].infoDictionary[key];
    
    NSString *version = [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
    //根据版本号来判断是否需要显示引导页，一般来说每更新一个版本引导页都会有相应的修改
    BOOL show = [YMSaveTool boolForKey:[NSString stringWithFormat:@"version_%@", version]];
    
    //NSLog(@"%@",[SYDAccountTool getToken]);
    //判断有无token 和时间是否正确
    SYDAccount *account = [SYDAccountTool getAccount];
    if ([SYDAccountTool getToken] == nil||[[SYDAccountTool getToken]isEqualToString:@""]) {
        SYDLoginViewController *loginVc = [[SYDLoginViewController alloc]init];
        YMNavigationController *nav = [[YMNavigationController alloc]initWithRootViewController:loginVc];
        self.rootViewController = nav;
    }else if (account.mobilephone == nil || [account.mobilephone isEqualToString:@""]){
        SYDLoginViewController *loginVc = [[SYDLoginViewController alloc]init];
        YMNavigationController *nav = [[YMNavigationController alloc]initWithRootViewController:loginVc];
        self.rootViewController = nav;
    }else{
        //验证时间是否有效
        //NSLog(@"有效期：%@",[YMSaveTool valueForKey:Invaild_KEY]);
        //NSLog(@"-----%d",[self compareDate:[NSString getCurrentTimeDate] withDate:[YMSaveTool valueForKey:Invaild_KEY]]);
        int isLogin = [self compareDate:[NSString getCurrentTimeDate] withDate:[YMSaveTool valueForKey:Invaild_KEY]];
        if (isLogin == -1) {//超期限了
            SYDLoginViewController *loginVc = [[SYDLoginViewController alloc]init];
            YMNavigationController *nav = [[YMNavigationController alloc]initWithRootViewController:loginVc];
            self.rootViewController = nav;
        } else {
            
//            YMSettingViewController *vc = [[YMSettingViewController alloc] init];
//            self.rootViewController = [[YMNavigationController alloc] initWithRootViewController:vc];
            
            SYDHomeViewController *homeVc = [[SYDHomeViewController alloc]init];
            YMNavigationController *nav = [[YMNavigationController alloc]initWithRootViewController:homeVc];
            self.rootViewController = nav;
        }
    }
    if (show) {
        
    } else {
        // 将版本号存进沙盒
        [YMSaveTool setObject:currentVersion forKey:key];
    }
}
-(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
    int ci;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *dt1 = [[NSDate alloc] init];
    NSDate *dt2 = [[NSDate alloc] init];
    dt1 = [df dateFromString:date01];
    dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending: ci=1; break;
            //date02比date01小
        case NSOrderedDescending: ci=-1; break;
            //date02=date01
        case NSOrderedSame: ci=0; break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
    }
    return ci;
}
-(NSString *)timeDateWithTimeIntervalString:(double )lastLoginDate
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:lastLoginDate];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
- (NSDate *)getCurrentLocalDate:(NSDate *)date {
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    NSTimeZone *destinationTimeZone = [NSTimeZone localTimeZone];
    
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
    
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    
    NSDate *destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:date];
    
    return destinationDateNow;
}
//字符串转日期格式
- (NSDate *)stringToDate:(NSString *)dateString withDateFormat:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    
    NSDate *date = [dateFormatter dateFromString:dateString];
    return [self worldTimeToChinaTime:date];
}

//将世界时间转化为中国区时间
- (NSDate *)worldTimeToChinaTime:(NSDate *)date
{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    return localeDate;
}
- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}


@end
